<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="发送全服邮件">
        <el-switch
          v-model="isAllMail"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>
      <el-form-item v-if="isAllMail==false" label="玩家列表">
        <el-input v-model="users" placeholder="请输入标题" />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title" placeholder="请输入标题" />
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <el-input v-model="form.content" :autosize="{ minRows: 2, maxRows: 6}" type="textarea" placeholder="请输入内容" />
      </el-form-item>
      <el-form-item label="物品">
        <el-input v-model="form.res" :autosize="{ minRows: 2, maxRows: 6}" type="textarea" placeholder="请输入物品" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')">发送邮件</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import { getList, userSendMail } from '@/api/gameuser'
import { userSendMail } from '@/api/gameuser'

export default {
  data() {
    return {
      isAllMail: true,
      users: '',
      form: {
        title: '',
        content: '',
        res: '',
        uids: [],
        type: 1
      },
      rules: {
        title: [
          { required: true, message: '请输入邮件标题', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入邮件内容', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    clearMails() {
      this.form.title = ''
      this.form.content = ''
      this.form.res = ''
    },
    onSubmit(rform) {
      this.$refs[rform].validate((valid) => {
        if (valid) {
          var maildata = {
            type: 1,
            title: this.form.title,
            content: this.form.content,
            res: this.form.res
          }
          // 清理邮件表单数据
          this.clearMails()
          if (this.isAllMail === false) {
            this.$message({
              message: '该功能没有实现!',
              type: 'warning'
            })
            return
          }
          userSendMail(maildata).then(response => {
            this.$notify({
              title: '邮件',
              message: '发送邮件成功',
              type: 'success',
              duration: 2000
            })
          })
        } else {
          return false
        }
      })
    },
    onCancel() {
      // 清理邮件表单数据
      this.clearMails()
      this.$message({
        message: 'cancel!',
        type: 'warning'
      })
    }
  }
}
</script>

<style scoped>
.line{
  text-align: center;
}
</style>

